package com.leetcode.p100;

import java.util.Stack;

public class Test150 {

    public int evalRPN(String[] tokens) {
        Stack<String> stack = new Stack<>();
        int length = tokens.length;

        for (int i = 0; i < length; i++) {
            String current = tokens[i];
            if (current.equals("+")) {
                int a = Integer.valueOf(stack.pop());
                int b = Integer.valueOf(stack.pop());
                int c = a + b;
                stack.push(c + "");
            } else if (current.equals("*")) {
                int a = Integer.valueOf(stack.pop());
                int b = Integer.valueOf(stack.pop());

                int c = a * b;
                stack.push(c + "");
            } else if (current.equals("/")) {
                int a = Integer.valueOf(stack.pop());
                int b = Integer.valueOf(stack.pop());

                int c = b / a;
                stack.push(c + "");
            } else if (current.equals("-")) {
                int a = Integer.valueOf(stack.pop());
                int b = Integer.valueOf(stack.pop());

                int c = b - a;
                stack.push(c + "");
            }else {
                stack.push(tokens[i]);
            }
        }

        return Integer.valueOf(stack.pop());
    }

    public static void main(String[] args) {
        Test150 test = new Test150();
//        int result = test.evalRPN(new String[]{"2", "1", "+", "3", "*"});
        int result = test.evalRPN(new String[]{"10","6","9","3","+","-11","*","/","*","17","+","5","+"});
        System.out.println(result);
    }

}
